<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use App\Repositories\Admin\HistoryRepository;
use App\Repositories\Admin\PictureRepository;
use App\Repositories\Admin\UserRepository;

class UserController extends Controller
{
    protected $pictureRepository;
    protected $historyRepository;
    protected $userRepository;
    public function __construct(PictureRepository $pictureRepository)
    {
        $historyRepository = new HistoryRepository();
	$userRepository = new UserRepository();
        $this->historyRepository = $historyRepository;
        $this->pictureRepository = $pictureRepository;
	$this->userRepository = $userRepository;
    }
    public function getUsersPicture(Request $request){
        $userId = $request->get('user_id');
        $pictures = $this->pictureRepository->getByUserId($userId);
        $maxBlood =  Redis::get('maxBlood');
        $addPictureBlood =  Redis::get('addPictureBlood');
        return json_encode(['code'=>200,'pictures'=>$pictures,'maxBlood'=>$maxBlood,'addBlood'=>$addPictureBlood]);
    }

    //分享后用户增加的票
    public function addTicket(Request $request){
        $userId = $request->get('userId');
        $addTickeetNumber = Redis::get('addUserTicket');
        $user = $this->userRepository->getById($userId);
        $ticket = $user['user_ticket'] + $addTickeetNumber;
        $allTicket = $user['user_all_ticket'] + $addTickeetNumber;
        $updateData = [
            'user_all_ticket' => $allTicket,
            'user_ticket' => $ticket
        ];
	    $result = $this->userRepository->updateUser($userId,$updateData);
        return json_encode(['code'=>200,'ticket'=>$ticket,'allTicket'=>$allTicket]);
    }
}
